import { loginByEmail, logout, getInfo, getMenus } from "api/login";
import { getToken, setToken, removeToken } from "utils/auth";
import { hydrantRouterMap } from "@/assets/schedule/sidebar.js";
const user = {
    state: {
        user: "",
        status: "",
        code: "",
        token: getToken(),
        name: "",
        avatar: "",
        introduction: "",
        roles: [],
        menus: undefined,
        eleemnts: undefined, //这个不知道是啥
        elements: undefined,
        permissionMenus: undefined,
        setting: {
            articlePlatform: [],
        },
        //存到用户其他信息
        //组织机构
        Iframework: undefined,
        sys_Company_Code: "",
    },

    mutations: {
        SET_CODE: (state, code) => {
            state.code = code;
        },
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_INTRODUCTION: (state, introduction) => {
            state.introduction = introduction;
        },
        SET_SETTING: (state, setting) => {
            state.setting = setting;
        },
        SET_STATUS: (state, status) => {
            state.status = status;
        },
        SET_NAME: (state, name) => {
            state.name = name;
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar;
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles;
        },
        SET_MENUS: (state, menus) => {
            state.menus = menus;
        },
        SET_ELEMENTS: (state, elements) => {
            state.elements = elements;
        },
        LOGIN_SUCCESS: () => {},
        LOGOUT_USER: (state) => {
            state.user = "";
        },
        SET_PERMISSION_MENUS: (state, permissionMenus) => {
            console.log(permissionMenus, "88888888888888888888888888888")

            // console.log(scheduleRouterMap)
            // state.permissionMenus = hydrantRouterMap;
            state.permissionMenus = permissionMenus;


        },
    },

    actions: {
        // 邮箱登录
        LoginByEmail({ commit }, userInfo) {
            const username = userInfo.username.trim();
            console.log(userInfo, 35888);
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            commit("SET_MENUS", undefined);
            commit("SET_ELEMENTS", undefined);
            removeToken();
            return new Promise((resolve, reject) => {
                loginByEmail(username, userInfo.password, userInfo.verification)
                    .then((response) => {
                        setToken(response.data);
                        commit("SET_TOKEN", response.data);
                        // debugger
                        resolve();
                    })
                    .catch((error) => {
                        // debugger
                        reject(error);
                    });
            });
        },

        // 获取用户信息
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                let st;
                getInfo(state.token)
                    .then((response) => {
                        //console.log(user)
                        // st=response
                        //user['impData']=response
                        user.state["id"] = response.id;
                        user.state["sys_Company_Code"] = response.sysCompanyCode;
                        // console.log(user.impData.sysCompanyCode)
                        //this.$store.state['useData']=response
                        // console.log(this.$store)

                        const data = response;
                        //debugger
                        commit("SET_ROLES", "admin");
                        commit("SET_NAME", data.name);
                        commit(
                            "SET_AVATAR",
                            "http://www.icosky.com/icon/png/Application/Office%202008%20Mono%20-%20Mac%20icns/MSN%2708.png"
                        );
                        commit("SET_INTRODUCTION", data.description);
                        console.log('查询页用户：', data);
                        const menus = {};
                        for (let i = 0; i < data.menus.length; i++) {
                            menus[data.menus[i].code] = true;
                        }
                        commit("SET_MENUS", menus);
                        const elements = {};

                        for (let i = 0; i < data.elements.length; i++) {
                            elements[data.elements[i].code] = true;
                        }
                        commit("SET_ELEMENTS", elements);
                        resolve(response);
                    })
                    .catch((error) => {
                        reject(error);
                    });
                // console.log(user.impData.sysCompanyCode)
                //debugger
                //  if(user.state['sys_Company_Code']){
                getMenus(state.token, user.state["sys_Company_Code"]).then(
                    (response) => {
                        // console.log(response[0].children[0].children[0].title)
                        //response[0].children[0].children[0].title='用户管理666666'

                        //debugger
                        commit("SET_PERMISSION_MENUS", response);
                    }
                );
                // }
                // alert('ok')
                //debugger
                //this.$store.state['sys_Company_Code']=response.sysCompanyCode
                //  console.log(this.$store.state)
                //   alert('1111112233')
            });
        },

        // 第三方验证登录
        LoginByThirdparty({ commit, state }, code) {
            return new Promise((resolve, reject) => {
                commit("SET_CODE", code);
                loginByThirdparty(state.status, state.email, state.code)
                    .then((response) => {
                        commit("SET_TOKEN", response.data.token);
                        setToken(response.data.token);
                        resolve();
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },

        // 登出
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                logout(state.token)
                    .then(() => {
                        commit("SET_TOKEN", "");
                        commit("SET_ROLES", []);
                        commit("SET_MENUS", undefined);
                        commit("SET_ELEMENTS", undefined);
                        commit("SET_PERMISSION_MENUS", undefined);
                        removeToken();
                        resolve();
                    })
                    .catch((error) => {
                        reject(error);
                    });
            });
        },

        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise((resolve) => {
                commit("SET_TOKEN", "");
                commit("SET_MENUS", undefined);
                commit("SET_ELEMENTS", undefined);
                commit("SET_PERMISSION_MENUS", undefined);
                removeToken();
                resolve();
            });
        },

        // 动态修改权限
        ChangeRole({ commit }, role) {
            return new Promise((resolve) => {
                commit("SET_ROLES", [role]);
                commit("SET_TOKEN", role);
                setToken(role);
                resolve();
            });
        },
    },
};

export default user;